﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Drawing;
using System.IO;

namespace Pic.ThumbService
{
    /// <summary>
    /// thumb 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://Thumb.TTTV.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class thumb : System.Web.Services.WebService
    {
        [WebMethod]
        public string ThumbUrl(string proName, string fileUrl, int width, int height)
        {
            try
            {
                Create_Directory(Server.MapPath("upload\\" + proName));
                string temp = FormatDate2DirectoryName();
                Create_Directory(Server.MapPath("upload\\" + proName + "\\" + temp));
                string path = "upload\\" + proName + "\\" + temp + "\\";
                string fileName = Make_FileName();
                ImageClass img = new ImageClass(fileUrl);
                bool succ = img.GetReducedImage(width, height, Server.MapPath(path + fileName + ".jpg"));
                if (succ)
                {
                    return "http://" + HttpContext.Current.Request.Url.Authority + "/upload/" + proName + "/" + temp + "/" + fileName + ".jpg";
                }

            }
            catch
            {
                return "http://" + HttpContext.Current.Request.Url.Authority + "/error.jpg";
            }
            return "http://" + HttpContext.Current.Request.Url.Authority + "/error.jpg";
        }

        #region 跟据当前时间加上四位随机数生成一个文件名
        public static string Make_FileName()
        {
            Random random1 = new Random();
            string text1 = DateTime.Now.DayOfYear.ToString() + DateTime.Now.Minute.ToString();
            text1 = text1.Replace("-", "").Replace(":", "").Replace("PM", "").Replace("AM", "").Replace("\u4e0a\u5348", "").Replace("\u4e0b\u5348", "").Replace(" ", "");
            int num1 = random1.Next(0x3e8, 0x270f);
            return (text1 + num1.ToString());
            //return Guid.NewGuid().ToString();
        }
        #endregion

        #region 新建一个空文件夹
        public static bool Create_Directory(string path)
        {
            bool flag1 = false;
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                flag1 = true;
            }
            catch (Exception)
            {
                flag1 = false;
            }
            return flag1;
        }
        #endregion

        #region 将一个数字日期转换成文件名，前面不足的补零 FormatDateTime2FileNameString
        public static string FormatDateTime2FileNameString()
        {
            string s;
            DateTime thedate = DateTime.Now;
            s = FormatNumber2String(thedate.Year, 4) + FormatNumber2String(thedate.Month, 2) + FormatNumber2String(thedate.Day, 2) + FormatNumber2String(thedate.Hour, 2) + FormatNumber2String(thedate.Minute, 2) + FormatNumber2String(thedate.Second, 2);
            return s;
        }

        public static string FormatDateTime2FileNameString(System.DateTime SelectedDate)
        {
            string s;
            DateTime thedate = SelectedDate;
            s = FormatNumber2String(thedate.Year, 4) + FormatNumber2String(thedate.Month, 2) + FormatNumber2String(thedate.Day, 2) + FormatNumber2String(thedate.Hour, 2) + FormatNumber2String(thedate.Minute, 2) + FormatNumber2String(thedate.Second, 2);
            return s;
        }

        public static string FormatDate2DirectoryName()
        {
            string s;
            DateTime thedate = DateTime.Now;
            s = FormatNumber2String(thedate.Year, 4) + FormatNumber2String(thedate.Month, 2) + FormatNumber2String(thedate.Day, 2);
            return s;
        }

        public static string FormatDate2DirectoryName(System.DateTime SelectedDate)
        {
            string s;
            DateTime thedate = SelectedDate;
            s = FormatNumber2String(thedate.Year, 4) + FormatNumber2String(thedate.Month, 2) + FormatNumber2String(thedate.Day, 2);
            return s;
        }

        public static string FormatDate2String(System.DateTime theDate)
        {
            string s;
            s = FormatNumber2String(theDate.Year, 4) + "-" + FormatNumber2String(theDate.Month, 2) + "-" + FormatNumber2String(theDate.Day, 2);
            return s;
        }

        public static string FormatNumber2String(long intNumber, Int16 intLength)
        {
            string s;
            s = intNumber.ToString();
            while (s.Length < intLength)
            {
                s = "0" + s;
            }
            return s;
        }
        #endregion

    }
}
